package com.sw.haruka.ui.widget

import android.widget.Toast
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Scaffold
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.ExperimentalUnitApi
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.TextUnitType
import com.sw.haruka.App
import com.sw.haruka.R

@OptIn(ExperimentalUnitApi::class)
@Composable
fun PageWithBack(title:String, backClickListener: ()->Unit, body: @Composable ()->Unit) {
    Scaffold(topBar = {
        Row(
            modifier = Modifier.height(dimensionResource(id = R.dimen.topBarHeight)),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Image(
                painter = painterResource(id = R.drawable.ic_arrow_left),
                contentDescription = "back button",
                contentScale = ContentScale.FillHeight,
                modifier = Modifier
                    .fillMaxHeight()
                    .clickable {
                        backClickListener()
                    }
            )
            Text(
                text = title,
                fontSize = TextUnit(dimensionResource(id = R.dimen.topBarTitleTextSize).value, TextUnitType.Sp),
            )
        }
    }) { padding ->
        Surface(modifier = Modifier.padding(padding)) {
            body()
        }
    }
}

@Preview(showSystemUi = true, showBackground = true)
@Composable
fun Test() {
    PageWithBack(title = "FTP", backClickListener = {
        Toast.makeText(App.instance(), "clicked", Toast.LENGTH_SHORT).show()
    }) {

    }
}